package com.sky.controller.admin;

import com.sky.controller.BaseController;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageDTO;
import com.sky.entity.SetmealEntity;
import com.sky.enumeration.SetmealStatusEnum;
import com.sky.result.Result;
import com.sky.result.TableDataInfo;
import com.sky.service.SetmealService;
import com.sky.utils.TransformUtils;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@RestController("AdminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController extends BaseController {

    @Autowired
    private SetmealService setmealService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐信息 =>{}", setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public TableDataInfo page(SetmealPageDTO pageDTO, @RequestParam(required = false) String status) {
        SetmealStatusEnum setmealStatusEnum = TransformUtils.transformEnum(status, SetmealStatusEnum.class);
        log.info("套餐分页查询 =>{}", pageDTO);
        log.info("套餐分页查询状态 =>{}", setmealStatusEnum);
        List<SetmealEntity> page = setmealService.page(pageDTO, setmealStatusEnum);
        return getTableInfo(page);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐信息 =>{}", setmealDTO);
        setmealService.update(setmealDTO);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("根据id查询套餐=>{}", id);
        SetmealVO byIdWithDish = setmealService.getByIdWithDish(id);
        return Result.success(byIdWithDish);
    }

    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable String status, @RequestParam Long id) {
        SetmealStatusEnum statusEnum = TransformUtils.transformEnum(status, SetmealStatusEnum.class);
        setmealService.startOrStop(statusEnum, id);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        setmealService.deleteBatch(ids);
        return Result.success();
    }

}
